package core

import (
	"LearnProject/conf"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/utils"
)

func InitMysql(config *conf.MysqlConfigStruct) *gorm.DB {
	dsn := config.Username + ":" + config.Password + "@tcp(" + config.Host + ":" + utils.ToString(config.Port) +")/" + config.Database + "?charset=utf8mb4&parseTime=True&loc=Local"
	fmt.Println(dsn)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
	} else {
		fmt.Println("数据库连接 OK")
	}

	return db
}
